$(document).ready(function(){

	$('.full-search-switcher').click(function(){
			if($('.full-search').is(':visible'))
			{
				$('.full-search-switcher').removeClass('switcher-close');
				$('.full-search-switcher').addClass('switcher-open');
			}
			else
			{				
				$('.full-search-switcher').removeClass('switcher-open');
				$('.full-search-switcher').addClass('switcher-close');
			}
			
			$('.full-search').slideToggle();
	});
	
	$('.full-search > .switcher > span').click(function(){
		var id = $(this).attr('rel');
		$('.full-search > .switcher > span').removeClass('on');
		$(this).addClass('on');
		
		$('.full-search .page').hide();
		$('.full-search .page[rel='+id+']').show();
	});
	
	
	/*$('.tools > ul > li > a').click(function(){
		
		if($(this).parent().hasClass('active'))
		{
			$('.tools > ul > li > a').parent().removeClass('active');
			$('.tools > ul > li > a').parent().find('.container').hide();
		}
		else
		{
			$('.tools > ul > li > a').parent().removeClass('active');
			$('.tools > ul > li > a').parent().find('.container').hide();
			
			$(this).parent().addClass('active');
			$(this).parent().find('.container').show();
		}
	});*/
	
	// Спрятать сайдбар
	$('.search-container > .hide').click(function(){
		 $('.sidebar').hide();
		 $('.show-sidebar').show();
		 $('#map_toolbar').removeClass('map_toolbar_leftpanel_on');
		 $('#map_toolbar').addClass('map_toolbar_leftpanel_off');
		 //$('.map').addClass('map-close');
		 //$('.map').removeClass('map-open');
		// resize();
	});
	
	
	// Показать сайдбар
	$('.show-sidebar').click(function(){
		$('.sidebar').show();
		$('.show-sidebar').hide();
		$('#map_toolbar').addClass('map_toolbar_leftpanel_on');
		 $('#map_toolbar').removeClass('map_toolbar_leftpanel_off');
		//$('.map').removeClass('map-close');
		//$('.map').addClass('map-open');
		//resize();
	});
	
	
	$('#map_search_btn').click(function(){
		var s = trim($('#map_search').val());		
		if(is_cadnum(s)) searchCadnum(s);
		else searchGoggle(s);
	});
	
	$('#map_search').keypress(function(event) {
		  if ( event.which == 13 ) {
			  var s = trim($('#map_search').val());		
				if(is_cadnum(s)) searchCadnum(s);
				else searchGoggle(s);
		  }//else event.preventDefault();		
	});
	
	var geoCoder = new google.maps.Geocoder();
	
	$('#map_search').autocomplete({
		source: function(request, response) {
		geoCoder.geocode( {'address': request.term }, function(results, status) {
			response($.map(results, function(item) {
				return {
					label: item.formatted_address,
					value: item.formatted_address,
					latitude: item.geometry.location.lat(),
					longitude: item.geometry.location.lng()
				}
			}));
		})
		},	
		select: function(event, ui) {
			//$("#latitude").val(ui.item.latitude);
			//$("#longitude").val(ui.item.longitude);
			//var location = new google.maps.LatLng(ui.item.latitude, ui.item.longitude);
			//marker.setPosition(location);
			//map.setCenter(location);
		}
	});

	
	
	$('#is_agree').click(function(){
		if($(this).is(':checked')) $('#btn_feedback_send').attr('disabled', false);
		else  $('#btn_feedback_send').attr('disabled', true);
	});
	
	$('#chk_feedback_no_errors').click(function(){
		if($(this).is(':checked'))
		{
			$('input.problem').attr('disabled', true);
			$('input.problem').attr('checked', false);
		}
		else  $('input.problem').attr('disabled', false);
	});
	
	
	$('#map_control_print').click(function(){
		var url = $('#permalink a').attr('href');		
		var aurl = explode('?', url);
		
		
		window.open('/kadastrova-karta/print?'+aurl[1], 'mywindow','width=600,height=600,resizable=no,scrollbars=no,toolbar=no,location=no,directories=no,status=no,menubar=no,copyhistory=no');
	});
	
	$('#btn_feedback_send').click(function(){
		var cadnum = $('#popup_feedback input[name=cadnum]').val();
		
		var no_parcel = $('#popup_feedback input[name=no_parcel]').is(':checked');	
		var no_cadnum = $('#popup_feedback input[name=no_cadnum]').is(':checked');
		var error_location = $('#popup_feedback input[name=error_location]').is(':checked');
		var error_configuration = $('#popup_feedback input[name=error_configuration]').is(':checked');
		var error_use = $('#popup_feedback input[name=error_use]').is(':checked');
		var error_cross = $('#popup_feedback input[name=error_cross]').is(':checked');
		var no_errors = $('#popup_feedback input[name=no_errors]').is(':checked');
		
		var last_name = $('#popup_feedback input[name=last_name]').val();
		var first_name = $('#popup_feedback input[name=first_name]').val();
		var middle_name = $('#popup_feedback input[name=middle_name]').val();
		
		var pass_series = $('#popup_feedback input[name=pass_series]').val();
		var pass_number = $('#popup_feedback input[name=pass_number]').val();
		var identification_number = $('#popup_feedback input[name=identification_number]').val();
		var phone = $('#popup_feedback input[name=phone]').val();
		var email = $('#popup_feedback input[name=email]').val();
		var addr = $('#popup_feedback input[name=addr]').val();
		
		if(trim(cadnum) == '')
		{
			alert("Необхідно ввести Кадастровий номер");
			return false;
		}
		
		if(trim(last_name) == '')
		{
			alert("Необхідно ввести Фамілію");
			return false;
		}
		
		if(trim(first_name) == '' )
		{
			alert("Необхідно ввести Ім'я");
			return false;
		}
		
		if(trim(middle_name) == '' )
		{
			alert("Необхідно ввести По батькові");
			return false;
		}	
		
		if(trim(pass_series) == '' || trim(pass_number) == '')
		{
			alert("Необхідно ввести Серію та номер паспорта");
			return false;
		}	
		
		if(trim(identification_number) == '' )
		{
			alert("Необхідно ввести ІНН");
			return false;
		}
		
		if(trim(phone) == '' )
		{
			alert("Необхідно ввести Телефон");
			return false;
		}	
		
		if(trim(email) == '' )
		{
			alert("Необхідно ввести E-mail");
			return false;
		}	
		
		if(trim(addr) == '' )
		{
			alert("Необхідно ввести Адресу");
			return false;
		}				
		
		
		$.ajax({
			url: '/kadastrova-karta/submit-app', 
			dataType: 'json',
			async: false,
			data: {
				cadnum: cadnum,
				no_parcel: no_parcel,
				no_cadnum: no_cadnum,
				error_location: error_location,
				error_configuration: error_configuration,
				error_use: error_use,
				error_cross: error_cross,
				no_errors: no_errors,
				last_name: last_name,
				first_name: first_name,
				middle_name: middle_name,
				pass_series: pass_series,
				pass_number: pass_number,
				identification_number: identification_number,
				phone: phone,
				email: email,
				addr: addr
			},
			success: function(data){
				if(data['status']){
					$('.editlist input[type=text]').val('');
					$('.editlist input[type=checkbox]').attr('checked', false);
					$('#btn_feedback_send').attr('disabled', true);					
					popup.close();
				}
				
				alert(data['msg']);
			}			
		});
	});
	
	
	
});